package com.homedroid.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.homedroid.dal.Product;
import com.homedroid.dal.R;

public class MyList extends Activity {

	private Button back;
	private ArrayList<String> saved = new ArrayList<String>();
	public static boolean done = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mylist);

		this.back = (Button) findViewById(R.id.btn_back_to_list);
		this.back.setOnClickListener(listener);
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			String buffer = extras.getString("buffer");
			String[] bufferedlist = buffer.split(":");
			for(int i = 1; i < bufferedlist.length; i++){
				saved.add(bufferedlist[i]);
			}
			
			
		}
		Main();
	}
	
	@Override
	public void onBackPressed() {
		List1.product = new Product();
		done=false;
		finish();
		super.onBackPressed();
	}

	public Button.OnClickListener listener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_back_to_list:
				List1.product = new Product();
				done=false;
				finish();
				break;

			default:
				break;
			}
		}

	};
	
	public void Main(){
    	final ListView list = (ListView)findViewById(R.id.mylistview);
    	String [] lv_arr = saved.toArray(new String[saved.size()]);
    	list.setAdapter(new ArrayAdapter<String>(this,R.layout.list_view_green , lv_arr));
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> a, View v,final int position, long id) {
				AlertDialog.Builder adb = new AlertDialog.Builder(MyList.this);
				adb.setTitle("Options");
				adb.setMessage(list.getItemAtPosition(position).toString());
				adb.setPositiveButton("Ok", null);
				adb.setNegativeButton("Delete", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						saved.remove(position);
						Main();
					}
				});
				adb.show();
			}
		});
	}

}
